package com.codeant.bikeindicatorapplication.tools;

import android.Manifest;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;

public class PublicVariables {
    /*公用、重复的一些变量、方法*/

    //几个可以点击的控件的TAG
    public final Integer TAG_SBTN_SETTING = 1;
    public final Integer TAG_SBTN_BLE_STATUS = 5;
    public final Integer TAG_SBTN_BLE_CONNECT = 10;
    public final Integer TAG_SBTN_AUTO_MODE = 15;
    public final Integer TAG_SBTN_WAKE_SCREEN = 20;
    public final Integer TAG_SBTN_MSG = 25;

    //熄停左右的4钟状态标志
    public final Integer TAG_LEFT = 101;//01
    public final Integer TAG_RIGHT = 102;//02
    public final Integer TAG_STOP = 103;//03
    public final Integer TAG_RESET = 104;//04
    public final Integer TAG_HONKING = 105;//05

    public final String STR_BLE_ON = "蓝牙打开";
    public final String STR_BLE_OFF = "蓝牙关闭";
    public final String STR_BLE_CONNECT = "已连接";
    public final String STR_BLE_DISCONNECT = "未连接";

    //部分权限列表
    public static String[] getPerms(){
        return new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.VIBRATE,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.CAMERA,

        };
    }

    //重启应用
    public static void restart(Context context, Activity activity) {
        int delayMillis = 100;
        // SingleToast.getInstance(activity).show("应用将在"+delayMillis/1000+"秒内重启");
        SingleToast.getInstance(activity).show("应用即将重启");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // 获取启动的intent
                Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
                /*PendingIntent的4种Flags
                * FLAG_CANCEL_CURRENT:如果要创建的PendingIntent已经存在了，那么在创建新的PendingIntent之前，原先已经存在的PendingIntent中的intent将不能使用
                * FLAG_NO_CREATE:如果要创建的PendingIntent尚未存在，则不创建新的PendingIntent，直接返回null
                * FLAG_ONE_SHOT:相同的PendingIntent只能使用一次，且遇到相同的PendingIntent时不会去更新PendingIntent中封装的Intent的extra部分的内容
                * FLAG_UPDATE_CURRENT:如果要创建的PendingIntent已经存在了，那么在保留原先PendingIntent的同时，将原先PendingIntent封装的Intent中的extra部分替换为现在新创建的PendingIntent的intent中extra的内容*/
                PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
                // 设置杀死应用后delayMillis秒重启
                AlarmManager mgr = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
                mgr.set(AlarmManager.RTC, System.currentTimeMillis() + delayMillis, restartIntent);
                // 重启应用
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        }, delayMillis);
    }

    //bytes打印测试
    public static final String encodeHex(byte[] bytes) {
        StringBuffer buff = new StringBuffer(bytes.length * 2);
        String b;
        for (int i = 0; i < bytes.length; i++) {
            b = Integer.toHexString(bytes[i]);
            // byte是两个字节的， 而上面的Integer.toHexString会把字节扩展为4个字节
            buff.append(b.length() > 2 ? b.substring(6, 8) : b);
            buff.append(" ");
        }
        return buff.toString();
    }
    public static final String arrayToString(byte[] bytes) {
        StringBuffer buff = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            buff.append(bytes[i] + " ");
        }
        return buff.toString();
    }
}
